home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
FROMUTS
/
UNIXLIB37B
/
src
/
unix
/
c
/
dev
< prev
next >
Wrap
Text File
|
1992-02-18
|
5KB
|
270 lines
static char sccs_id[] = "@(#) dev.c 2.0 "__DATE__" HJR";
/* dev.c (c) Copyright 1990 H.Rogers */
#include <errno.h>
#include <stdlib.h>
#include "fcntl.h"
#include "sys/unix.h"
#include "sys/param.h"
#include "sys/dev.h"
#include "sys/os.h"
struct dev __dev[NDEV] =
{
{ __fsopen,__fsclose,__fsread,__fswrite,__fslseek,__fsioctl },
{ __ttyopen,__ttyclose,__ttyread,__ttywrite,__ttylseek,__ttyioctl },
{ __pipeopen,__pipeclose,__piperead,__pipewrite,__pipelseek,__pipeioctl },
{ __nullopen,__nullclose,__nullread,__nullwrite,__nulllseek,__nullioctl }
};
int __fsopen(char *file,int mode,struct file *f)
{
register int oflag = f->oflag;
register int *r = f->r;
os_error *e;
int fd;
file = __uname(file,oflag & (O_CREAT|O_OMASK));
if (e = os_file(0x05,file,r))
{
__seterr(e);
return(-1);
}
if (r[0])
{
if ((oflag & (O_EXCL|O_CREAT)) == (O_EXCL|O_CREAT))
{
errno = EEXIST;
return(-1);
}
if (oflag & O_CREAT) oflag &= ~O_CREAT;
if (oflag & O_OMASK)
if (e = os_file(0x09,file,r))
{
__seterr(e);
return(-1);
}
}
else
{
if ((oflag & (O_RDONLY|O_CREAT)) == O_RDONLY)
{
errno = ENOENT;
return(-1);
}
if (oflag & O_OMASK) oflag |= O_CREAT;
}
if (oflag & O_CREAT)
{
r[2] = (oflag & O_BINARY) ? 0xffd : 0xfff; r[4] = r[5] = 0;
if (e = os_file(0x0b,file,r))
{
__seterr(e);
return(-1);
}
mode &= ~(__u->umask & 0777);
r[5] = ((mode & 0400)>>8) | ((mode & 0200)>>6) |
((mode & 0004)<<2) | ((mode & 0002)<<4);
if (e = os_file(0x04,file,r))
{
__seterr(e);
return(-1);
}
}
if (e = os_file(0x05,file,r))
{
__seterr(e);
return(-1);
}
if (oflag & O_TRUNC)
e = os_fopen(0x80,file,&fd);
else
switch (oflag & O_OMASK)
{
case O_RDONLY:
e = os_fopen(0x40,file,&fd);
break;
case O_WRONLY:
case O_RDWR:
e = os_fopen(0xc0,file,&fd);
break;
default:
errno = EINVAL;
return(-1);
break;
}
if (e)
{
__seterr(e);
return(-1);
}
return(fd);
}
int __fsclose(int fd,struct file *f)
{
os_error *e;
if (e = os_fclose(fd))
{
__seterr(e);
return(-1);
}
return(0);
}
int __fsread(int fd,register void *data,register int nbyte,struct file *f)
{
int r[5];
os_error *e;
if (e = os_fread(fd,data,nbyte,r))
{
__seterr(e);
return(-1);
}
return(nbyte - r[3]);
}
int __fswrite(int fd,register void *data,register int nbyte,struct file *f)
{
int r[5];
os_error *e;
if (e = os_fwrite(fd,data,nbyte,r))
{
__seterr(e);
return(-1);
}
return(nbyte - r[3]);
}
long __fslseek(int fd,long lpos,int whence,struct file *f)
{
int r[3];
os_error *e;
switch (whence)
{
case 0:
if (e = os_args(1,fd,(int)lpos,r))
{
__seterr(e);
return(-1);
}
break;
case 1:
if (e = os_args(0,fd,0,r))
{
__seterr(e);
return(-1);
}
if (e = os_args(1,fd,r[2] + (int)lpos,r))
{
__seterr(e);
return(-1);
}
break;
case 2:
if (e = os_args(2,fd,0,r))
{
__seterr(e);
return(-1);
}
if (e = os_args(1,fd,r[2] + (int)lpos,r))
{
__seterr(e);
return(-1);
}
break;
default:
errno = EINVAL;
return(-1);
break;
}
return((long)r[2]);
}
int __fsioctl(int fd,register int request,void *arg,struct file *f)
{ errno = EINVAL; return(-1); }
struct pipe *__pipe;
int __pipeopen(char *file,int mode,struct file *f)
{
return (__fsopen(file,mode,f));
}
int __pipeclose(int fd,struct file *f)
{
struct pipe *pi = __pipe,*pi_ = 0;
while (pi)
{
if (pi->p[0] == f || pi->p[1] == f) break;
pi_ = pi; pi = pi->next;
}
if (!pi) { errno = EBADF; return(-1); }
if (pi_) pi_->next = pi->next; else __pipe = pi->next;
__fsclose(fd,f);
{
os_error *e;
if (e = os_fsctrl(27,__uname(pi->file,0),0,0642))
{ __seterr(e); return(-1); }
}
free(pi->file);
free(pi);
return(0);
}
int __piperead(int fd,void *data,int nbyte,struct file *f)
{
if (f->oflag & O_PIPE)
{
f->oflag &= ~O_PIPE;
__fslseek(fd,0,0,f);
}
return(__fsread(fd,data,nbyte,f));
}
int __pipewrite(int fd,void *data,int nbyte,struct file *f)
{
if (f->oflag & O_PIPE)
return(__fswrite(fd,data,nbyte,f));
else
{ errno = EPIPE; return(-1); }
}
long __pipelseek(int fd,long lpos,int whence,struct file *f)
{ errno = ESPIPE; return(-1); }
int __pipeioctl(int fd,register int request,void *arg,struct file *f)
{ errno = EINVAL; return(-1); }
int __nullopen(char *file,int mode,struct file *f) { return(0); }
int __nullclose(int fd,struct file *f) { return(0); }
int __nullread(int fd,void *data,int nbyte,struct file *f) { return(0); }
int __nullwrite(int fd,void *data,int nbyte,struct file *f) { return(nbyte); }
long __nulllseek(int fd,long lpos,int whence,struct file *f)
{ errno = ESPIPE; return(-1); }
int __nullioctl(int fd,int request,void *arg,struct file *f)
{ errno = EINVAL; return(-1); }